package org.dromara.web.controller.sz.policy;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.policy.Policy;
import org.dromara.system.domain.policy.bo.PolicyBo;
import org.dromara.system.domain.policy.vo.PolicyImportVo;
import org.dromara.system.domain.policy.vo.PolicyVo;
import org.dromara.system.service.policy.IPolicyService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 承保单主
 *
 * @author Lion Li
 * @date 2025-11-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sz/policy")
public class PolicyController extends BaseController {

    private final IPolicyService policyService;

    /**
     * 查询承保单主列表
     */
//    @SaCheckPermission("system:policy:list")
    @GetMapping("/list")
    @SaIgnore
    public TableDataInfo<PolicyVo> list(PolicyBo bo, PageQuery pageQuery) {
        return policyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出承保单主列表
     */
    @SaCheckPermission("system:policy:export")
    @Log(title = "承保单主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PolicyBo bo, HttpServletResponse response) {
        List<PolicyVo> list = policyService.queryList(bo);
        ExcelUtil.exportExcel(list, "承保单主", PolicyVo.class, response);
    }

    /**
     * 获取承保单主详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:policy:query")
    @GetMapping("/{id}")
    public R<PolicyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(policyService.queryById(id));
    }

    /**
     * 新增承保单主
     */
    @SaCheckPermission("system:policy:add")
    @Log(title = "承保单主", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PolicyBo bo) {
        return toAjax(policyService.insertByBo(bo));
    }

    /**
     * 修改承保单主
     */
    @SaCheckPermission("system:policy:edit")
    @Log(title = "承保单主", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PolicyBo bo) {
        return toAjax(policyService.updateByBo(bo));
    }

    /**
     * 删除承保单主
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:policy:remove")
    @Log(title = "承保单主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(policyService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file 导入文件
     */
    @Log(title = "承保单导入", businessType = BusinessType.IMPORT)
//    @SaCheckPermission("system:policy:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @SaIgnore
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<PolicyImportVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), PolicyImportVo.class, true);
        List<Policy> list = MapstructUtils.convert(excelResult.getList(), Policy.class);
        policyService.saveBatch(list);
        return R.ok(excelResult.getAnalysis());
    }
}
